<?php
error_reporting(E_ERROR);
//$select=angajator

	
function curl_get($url) {
	
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  
		  
return $data;		
}
 function cmp($a, $b) {
			if ($a == $b) {
				return 0;
			}
			return ($a > $b) ? -1 : 1;
		  }

switch($_GET[box]) {
	
	case 'box1':
		$url = 'http://rogovdata.cloudapp.net:8080/v1/RoGovOpenData/Posturi?$select=judet&format=json';
		
		$return = array();
		$data =  curl_get($url);
		  
		 
		
		  $data_array = json_decode($data,true);
		
		
		  foreach($data_array[d] as $linie)
			if(!isset($return)){
			  $return[$linie[judet]] = 1;
			} else{
			  $return[$linie[judet]] ++;
			}
		  uasort($return, 'cmp');
		
		  $new = array();
		  $k=0;
		  foreach($return as $key => $line){
			if ($k<=5){
			  $new[$key]=$line;
			}
			$k++;
		  }
		  echo json_encode($new);	
	break;
	case 'box2':
		$url = 'http://rogovdata.cloudapp.net:8080/v1/RoGovOpenData/Posturi?$select=angajator&format=json';
		
		$return = array();
		$data = curl_get($url);
		  
		 
		
		  $data_array = json_decode($data,true);
		
		
		  foreach($data_array[d] as $linie)
			if(!isset($return)){
			  $return[$linie["angajator"]] = 1;
			} else{
			  $return[$linie["angajator"]] ++;
			}
		  uasort($return, 'cmp');
		
		  $new = array();
		  $k=0;
		  foreach($return as $key => $line){
			if ($k<=5){
				$key_short = $key;
				if(strlen($key) > 10)
					$key_short =  substr($key,0,10)."...";
				$new[$key_short][0] = $line;
				$new[$key_short][1] = $key;
			}
			$k++;
		  }
		 echo json_encode($new);	
	break;
	case 'box3':
	$url = 'http://rogovdata.cloudapp.net:8080/v1/RoGovOpenData/Posturi?$select=tip&format=json';
		
		$return = array();
		$data = curl_get($url);
		  
		 
		
		  $data_array = json_decode($data,true);
		
		
		  foreach($data_array[d] as $linie)
			if(!isset($return)){
			  $return[$linie["tip"]] = 1;
			} else{
			  $return[$linie["tip"]] ++;
			}
		  uasort($return, 'cmp');
		
		  $new = array();
		  $k=0;
		  foreach($return as $key => $line){
			if ($k<=5){
				$key_short = $key;
				if(strlen($key) > 15)
					$key_short =  substr($key,0,15)."...";
				$new[$key_short][0] = $line;
				$new[$key_short][1] = $key;
			}
			$k++;
		  }
		 echo json_encode($new);	
	
	
	
	break;
		case 'box4':
	$url = 'http://rogovdata.cloudapp.net:8080/v1/RoGovOpenData/Posturi?$select=tipangajator&format=json';
		
		$return = array();
		$data = curl_get($url);
		  
		 
		
		  $data_array = json_decode($data,true);
		
		
		  foreach($data_array[d] as $linie)
			if(!isset($return)){
			  $return[$linie["tipangajator"]] = 1;
			} else{
			  $return[$linie["tipangajator"]] ++;
			}
		  uasort($return, 'cmp');
		
		  $new = array();
		  $k=0;
		  foreach($return as $key => $line){
			if ($k<=5){
				$key_short = $key;
				if(strlen($key) > 15)
					$key_short =  substr($key,0,15)."...";
				$new[$key_short][0] = $line;
				$new[$key_short][1] = $key;
			}
			$k++;
		  }
		 echo json_encode($new);	
	
	
	
	break;
	
}
//Judet - 

	?>
